//
//  MissionInfo.m
//  Trading365
//
//  Created by Jason on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ApproveMailShow.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "Session.h"

@implementation ApproveMailShow
@synthesize table;
@synthesize dictMsg;
@synthesize InfoListTitle,InfoListName,InfoSectionTitle;
@synthesize myTitle,mailId;

#define CELL_CONTENT_SPACE 5.0

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        m_bThreadFresh      = NO; //默认非线程刷新
        m_condition = [[NSCondition alloc]init];

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)dealloc
{
    [labContent release];
    
    [dictMsg release];
    [InfoListTitle release];
    [InfoListName release];
    [InfoSectionTitle release];
    [myTitle release];
    [mailId release];
    [table release];
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = myTitle;
    [self.table setBackgroundColor:BACKGROUND_COLOR];

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"主题",@"收发时间",@"发件人",@"收件人",nil];
    self.InfoListTitle = title_array; 
    [title_array release];
    
    NSArray *name_array = [[NSArray alloc] initWithObjects:@"subject",@"receive_send_time",@"sendaddress",@"receiveaddress",nil];
    self.InfoListName = name_array; 
    [name_array release];

    NSArray *title_array1 = [[NSArray alloc] initWithObjects:@"",@"邮件内容",nil];
    self.InfoSectionTitle = title_array1; 
    [title_array1 release];

    //////
    NSString *text_content = [dictMsg objectForKey:@"content_txt"];
    UIFont *font = [UIFont systemFontOfSize:17];
    CGSize size = [text_content sizeWithFont:font constrainedToSize:CGSizeMake(300.0f, 2500.0f) lineBreakMode:  UILineBreakModeWordWrap];
    
    float h=size.height>0?size.height:44.0;
    
    labContent = [[UILabel alloc]initWithFrame:CGRectMake(CELL_CONTENT_SPACE, CELL_CONTENT_SPACE, 290.0, h )];
    labContent.lineBreakMode = UILineBreakModeWordWrap;
    labContent.textAlignment = UITextAlignmentLeft;
    labContent.backgroundColor = [UIColor clearColor];
    labContent.numberOfLines = 0;
    labContent.font = font;
    labContent.text = text_content;

}

- (void)viewDidAppear:(BOOL)animated
{
    [self refreshData];
}

- (void)viewDidUnload
{
    [self setTable:nil];
//    if (m_condition rele)
//    [m_condition unlock];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[[TaskManager Share] GetTaskByType:GetApproveMailShow Delegate:self] cancel];    
}

-(void)viewDidDisappear:(BOOL)animated
{
//    [[[TaskManager Share] GetTaskByType:GetApproveMailShow Delegate:self] cancel];    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section > 0){
//        return labContent.frame.size.height + CELL_CONTENT_SPACE * 2;
        NSString* text = [dictMsg objectForKey:@"content_txt"];
        NSLog(@"text=%@",text);
        
        UIFont *textFont = [UIFont systemFontOfSize:14];
        CGSize size = [text sizeWithFont:textFont constrainedToSize:CGSizeMake(300.0f, 2500.0f) lineBreakMode:  UILineBreakModeCharacterWrap];
        
        CGFloat height=44.0f;
        if (size.height > 1)
            height = size.height + CELL_CONTENT_SPACE * 2;
        
        return height;

    }
    
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [self.InfoSectionTitle count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0)
        return [InfoListName count];
    else
        return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.InfoSectionTitle objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier: CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    
    if ([dictMsg count] < 1)
        return cell;
    
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    
    if (section == 0){
        cell.textLabel.text=[self.InfoListTitle objectAtIndex:row];
        cell.detailTextLabel.text = [dictMsg objectForKey:[self.InfoListName objectAtIndex:row]];
        
//        cell.detailTextLabel.
    }
 
    if (section == 1 && row == 0) {    
        if (m_bThreadFresh == YES) {
            NSString *text_content = [dictMsg objectForKey:@"content_txt"];
            UIFont *font = [UIFont systemFontOfSize:17];
            CGSize size = [text_content sizeWithFont:font constrainedToSize:CGSizeMake(300.0f, 2500.0f) lineBreakMode:  UILineBreakModeWordWrap];
            
            float h=size.height>0?size.height:44.0;
            
            labContent = [[UILabel alloc]initWithFrame:CGRectMake(CELL_CONTENT_SPACE, CELL_CONTENT_SPACE, 290.0, h )];
            labContent.lineBreakMode = UILineBreakModeWordWrap;
            labContent.textAlignment = UITextAlignmentLeft;
            labContent.backgroundColor = [UIColor clearColor];
            labContent.numberOfLines = 0;
            labContent.font = font;
            labContent.text = text_content;
        }
        [cell.contentView addSubview:labContent];
    }
    
    return cell;
    
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    return nil;
}

//
#pragma mark - UserTask

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) OnPostExcute
{    
    [super OnPostExcute];
}

-(void) DoInBackground
{
    [m_condition lock];
    m_bThreadFresh = YES;
    
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp;
    
    tmp = [xml getMailDetailsShow:mailId];
    
    dictMsg = [[NSMutableDictionary alloc] initWithDictionary:tmp];
//    NSLog(@"dictMsg=%@",dictMsg);
    
    [xml release];
    
    [m_condition unlock];
    [self.table reloadData];

}


#pragma mark - my function


-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetApproveMailShow Delegate:self] Excute];
}


@end
